package JAVACORE_TRAINING.ADVANCED.SWING;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class ImageTutorial extends JFrame {
	public ImageTutorial() {
		this.setTitle("Image tutorial");
		this.setSize(400, 350);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JLabel jLabel = new JLabel();
		add(jLabel);
		
		jLabel.setToolTipText("This is tooltip");
		jLabel.setForeground(Color.BLUE);
		setPicture(jLabel, "src/JAVACORE_TRAINING/ADVANCED/SWING/Thread_Life_Cycle.jpg");
	}
	
	public void setPicture(JLabel jLabel, String filename){
		try {
			//jLabel.setSize(350, 300);
			BufferedImage bufferedImage = ImageIO
					.read(new File(filename));
			int x = jLabel.getSize().width;
			int y = jLabel.getSize().height;
			int ix = bufferedImage.getWidth();
			int iy = bufferedImage.getHeight();
			int dx = 0;
			int dy = 0;
			if (x / y > ix / iy) {
				dy = y;
				dx = dy * ix / iy;
			} else {
				dx = x;
				dy = dx * iy / ix;
			}
			ImageIcon icon = new ImageIcon(bufferedImage.getScaledInstance(dx, dy, bufferedImage.SCALE_SMOOTH));
			jLabel.setIcon(icon);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		ImageTutorial labelTutorial = new ImageTutorial();
	}
}
